using System.Collections.Generic;
using I2.Loc;
using UnityEngine;
using UnityEngine.Advertisements;
using UnityEngine.UI;

public class MainMenu : MonoBehaviour
{
	public static MainMenu Instance;

	public GameObject zombieLauncher;

	public float zombiePreMin = 60f;

	public Zombie zombie;

	public GameObject continueView;

	private float lastZombieLaunchTime;

	private List<Zombie> zombies = new List<Zombie>();

	public GameObject menuView;

	public GameObject namingView;

	public GameObject confirmNewCampaignView;

	public bool newCampaignConfirmed;

	public InputField namingInput;

	public Button namingButton;

	public Text nameText;

	public Text infoText;

	public AudioSource audioSource;

	public AudioClip failAudio;

	public Text versionText;

	public IAPManager iapManager;

	public Image moreGameBtnImg;

	private bool newCampaign;

	private void Awake()
	{
		if (!Instance)
		{
			Instance = this;
		}
		if (Config.initialized)
		{
			new Config().Init();
		}
		if (!DO.Player.initialized)
		{
			DO.Player.initialized = true;
			SaveLoad.SaveString("Player", JsonUtility.ToJson(DO.Player));
		}
		if (DO.Campaign.day == 1 && !DO.Campaign.dayFinish)
		{
			continueView.SetActive(value: false);
		}
		menuView.SetActive(value: true);
		namingView.SetActive(value: false);
		nameText.text = DO.Player.playerName;
	}

	private void Start()
	{
		new Config().Init();
		Advertisement.Banner.Hide();
		GameObject.Find("BackgroundMusic").GetComponent<BackgroundMusic>().active = true;
		versionText.text = "v" + Application.version;
		foreach (string availableItem in DO.Campaign.availableItems)
		{
			if (!DO.Player.reachedItems.Contains(availableItem))
			{
				DO.Player.reachedItems.Add(availableItem);
			}
		}
		SaveLoad.SaveString("Player", JsonUtility.ToJson(DO.Player));
	}

	public void NewCampaignConfirmed()
	{
		MyAdManager.Instance.MyShowNative();
		newCampaignConfirmed = true;
		NewCampaign();
	}

	public void NewCampaign()
	{
		MyAdManager.Instance.MyShowStartBox(() =>
		{
			if (DO.Campaign.totalIncome > 0 && !newCampaignConfirmed)
			{
				Text component = confirmNewCampaignView.transform.Find("Text").GetComponent<Text>();
				component.text = component.text.Replace("{day}", DO.Campaign.day.ToString());
				confirmNewCampaignView.SetActive(value: true);
				return;
			}
			newCampaign = true;
			DO.Player.playerName = "你";
			SaveLoad.SaveString("你", JsonUtility.ToJson(DO.Player));
			DO.NewCampaign();
			Initiate.Fade("CampaignGame", Color.black, 5f);
		});

	}
	public void ContinueCampaign()
	{
		MyAdManager.Instance.MyShowStartBox(() =>
		{
			DO.ReLoadCampaign();
			if (DO.Campaign.dayFinish && !DO.Campaign.dayShoppingFinish)
			{
				Initiate.Fade("CampaignShop", Color.black, 5f);
			}
			else
			{
				Initiate.Fade("CampaignPrepare", Color.black, 5f);
			}
		});
	}

	public void Playground()
	{
		MyAdManager.Instance.MyShowStartBox(() =>
		{
			Initiate.Fade("PlaygroundGame", Color.black, 5f);
		});
	}

	private void AddZombie()
	{
		Zombie zombie = Object.Instantiate(position: new Vector2(zombieLauncher.transform.position.x, zombieLauncher.transform.position.y + Random.Range(0f, 1.2f)), original: this.zombie, rotation: Quaternion.identity);
		zombie.body.audioSource.volume = 0.1f;
		zombies.Add(zombie);
	}

	private void FixedUpdate()
	{
		if (lastZombieLaunchTime + 60f / zombiePreMin < Time.time)
		{
			AddZombie();
			lastZombieLaunchTime = Time.time;
		}
		for (int i = 0; i < zombies.Count; i++)
		{
			if ((bool)zombies[i])
			{
				zombies[i].rb.velocity = new Vector2(0f - zombies[i].body.speed, 0f);
			}
			else
			{
				zombies.RemoveAt(i);
			}
		}
	}

	private void OnTriggerEnter2D(Collider2D collision)
	{
		Object.Destroy(collision.gameObject);
	}

	public void OpenURL(string url)
	{
		Application.OpenURL(url);
	}

	public void HowToPlay()
	{
		OpenURL(Config.urlHowToPlay);
	}

	public void Discord()
	{
		OpenURL(Config.urlDiscord);
	}

	public void MoreGame()
	{
		PlayerPrefs.SetString("urlMoreGameClicked", Config.urlMoreGame);
		OpenURL(Config.urlMoreGame);
	}

	public void EnableAdFree()
	{
		DO.Player.adFree = true;
		SaveLoad.SaveString("Player", JsonUtility.ToJson(DO.Player));
	}

	public void ResetAdFree()
	{
		DO.Player.adFree = false;
		SaveLoad.SaveString("Player", JsonUtility.ToJson(DO.Player));
	}

	public void QuitGame()
	{
		Application.Quit();
	}
}
